/*
 * @Author: modefy
 * @Date: 2023-04-26 17:10:32
 * @LastEditors: modefy
 * @LastEditTime: 2023-04-26 17:13:16
 * @Description: Description
 */
package com.example.learn.config;

import com.example.learn.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 设置拦截器.
 * 打上configuration 注解，标注为配置项
 *
 */
@Configuration
public class WebTokenMvcConfig implements WebMvcConfigurer {

    // 注入 token 拦截器
    @Autowired
    private TokenInterceptor interceptor;

    /**
     * 重写添加拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加自定义拦截器，并拦截对应 url
        registry.addInterceptor(interceptor).addPathPatterns("/**").excludePathPatterns("/admin/login","/admin/logout/", "/*/static/*" ,"/*/*.png", "/*.png", "/*/*.jpg", "/*.jpg");
    }

    /**
     * 指定静态资源的位置 非前后端分离项目需要注册
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /*静态资源的位置*/
//        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
//        registry.addResourceHandler("/**").addResourceLocations("classpath:/");
          registry.addResourceHandler("/static/**").
                addResourceLocations("file:"+System.getProperty("user.dir")+"/src/main/resources/static/");
    }

}